package com.hdoit.apollo.web.statistics.apis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.inStock.service.IInStockService;
import com.hdoit.apollo.logical.inventory.service.IInventoryService;
import com.hdoit.apollo.logical.statistics.model.StockChangeRecord;
import com.hdoit.apollo.logical.statistics.model.StockChangeStatistics;
import com.hdoit.apollo.logical.statistics.service.IStockChangeStatisticsService;
import com.hdoit.apollo.logical.stock.service.IOutStockService;
import com.hdoit.apollo.logical.viewmodel.statistics.StockChangeRecordSearch;
import com.hdoit.apollo.logical.viewmodel.statistics.StockChangeStatisticsSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.data.message.PagerModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/api/v1/stockChangeStatistics/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/stockChangeStatistics")
public class StockChangeStatisticsApi extends BaseApi{
	@Autowired
	private IStockChangeStatisticsService stockChangeStatisticsService;
	@Autowired
	private IInStockService inStockService;
	@Autowired
	private IOutStockService outStockService;
	@Autowired
	private IInventoryService inventoryService;
	
	private final String getStockChangeStatistics="getStockChangeStatistics";
	private final String getStockChangeRecord="getStockChangeRecord";
	private final String getDetail="getDetail";
	private final String getReturnRecord="getReturnRecord";
	
    @RequestMapping(value = getStockChangeStatistics)
    @ApiOperation(notes = getStockChangeStatistics, httpMethod = "GET", value = "获取库存变动统计信息")
    public @ResponseBody String getStockChangeStatistics(StockChangeStatisticsSearch search){
    	StockChangeStatistics stockChangeStatistics=stockChangeStatisticsService.getStockChangeStatistics(search);
    	ResponseModel rm=new ResponseModel();
    	rm.put("stockChangeStatistics",stockChangeStatistics);
    	return rm.toJson();
    }
    
    @RequestMapping(value = getStockChangeRecord)
    @ApiOperation(notes = getStockChangeRecord, httpMethod = "GET", value = "滚动获取库存变动记录信息")
    public @ResponseBody String getStockChangeRecord(StockChangeRecordSearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}
    	List<StockChangeRecord> stockChangeRecords=stockChangeStatisticsService.getStockChangeRecords(search,pagerModel);
  
    	rm.put("stockChangeRecords",stockChangeRecords);
    	return rm.toJson();
    }
    
    @RequestMapping(value = getReturnRecord)
    @ApiOperation(notes = getReturnRecord, httpMethod = "GET", value = "滚动获取发货记录信息")
    public @ResponseBody String getReturnRecord(StockChangeRecordSearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}
    	List<StockChangeRecord> stockChangeRecords=stockChangeStatisticsService.getReturnRecord(search,pagerModel);
  
    	rm.put("stockChangeRecords",stockChangeRecords);
    	return rm.toJson();
    }
    
    @RequestMapping(value = getDetail)
    @ApiOperation(notes = getDetail, httpMethod = "GET", value = "获取库存变动详细信息")
    public @ResponseBody String getDetail(String recordId,String recordType){
    	Object showData=new Object();
    	switch(recordType)
    	{
    	case "入库":
    		showData= inStockService.queryModelById(recordId);
    	  break;
    	case "采购退货":
    		showData= inStockService.queryModelById(recordId);
    	  break;
    	case "出库":
    		showData = outStockService.queryModelById(recordId);
    	  break;
	    case "发货":
			showData = outStockService.queryModelById(recordId);
		  break;
	    case "退货":
			showData = outStockService.queryModelById(recordId);
		  break;
	    case "盘点":
			showData = inventoryService.queryModelById(recordId);
		  break;
	    case "调整":
			showData = inventoryService.queryAdjustmentById(recordId);
		  break;
		}
    	ResponseModel rm=new ResponseModel();
    	rm.put("showData",showData);
    	return rm.toJson();
    }
}
